package com.smz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smz.entity.*;
import com.smz.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@Api
@RestController
public class TIomWarehouseController {
    @Autowired
    private TMenuService tMenuService;
    @Autowired
    private TReservoirService tReservoirService;
    @Autowired
    private TCargoService tCargoService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private TIomWarehouseService tIomWarehouseService;

    @ApiOperation("添加详细表")
    @PostMapping("/WarehouseDY")
    public ResponseEntity<String> wareHouse(@RequestBody TIomWarehouse tIomWarehouse) {
        System.out.println(tIomWarehouse);
        TCargo one1 = tCargoService.getOne(new QueryWrapper<TCargo>().lambda().eq(TCargo::getCatgoNo, tIomWarehouse.getIomOutcargoloca()));
        tIomWarehouse.setIomOutgoods(one1.getProduct());
        Commodity goods = commodityService.getOne(new QueryWrapper<Commodity>().lambda().eq(Commodity::getPName, tIomWarehouse.getIomGoods()));
        tIomWarehouse.setIomGoodsno(String.valueOf(goods.getPNumber()));
        Random random = new Random();
        String s = String.valueOf(random.nextInt(1000));
        tIomWarehouse.setIomNo(s);
        TMenu byId = tMenuService.getOne(new QueryWrapper<TMenu>().lambda().eq(TMenu::getWareNo, tIomWarehouse.getIomName()));
        tIomWarehouse.setIomName(byId.getWareName());
        TMenu byOutId = tMenuService.getOne(new QueryWrapper<TMenu>().lambda().eq(TMenu::getWareNo, tIomWarehouse.getIomOutname()));
        tIomWarehouse.setIomOutname(byOutId.getWareName());
        TReservoir one = tReservoirService.getOne(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getGoodsNo, tIomWarehouse.getIomArea()));
        tIomWarehouse.setIomArea(one.getGoodsName());
        TReservoir oneOut = tReservoirService.getOne(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getGoodsNo, tIomWarehouse.getIomOutarea()));
        tIomWarehouse.setIomOutarea(oneOut.getGoodsName());
        tIomWarehouseService.save(tIomWarehouse);
        return ResponseEntity.ok("ok");
    }
    @ApiOperation("查找一个")
    @GetMapping("/iomWare/{iomWare}")
    public ResponseEntity<TIomWarehouse> find(@PathVariable("iomWare") String num){
        TIomWarehouse one = tIomWarehouseService.getOne(new QueryWrapper<TIomWarehouse>().lambda().eq(TIomWarehouse::getNumber, num));
        return ResponseEntity.ok(one);
    }


}
